package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.anime.ppro.logic.AttendantComponent;
import cz.anime.ppro.logic.EventComponent;
import cz.anime.ppro.model.Attendant;
import cz.anime.ppro.model.Event;

@Component
@ManagedBean
@Scope("view")
public class CreateAttendantBean {
	
	private int eventID;
	
	private String name;
	private String contact;
	
	private Event event;
	
	
	@Autowired
	private AttendantComponent attendantComponent;
	
	@Autowired
	private EventComponent eventComponent;

	@PostConstruct
	public void init(){
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		Map<String,String[]> params = req.getParameterMap();
		
		if (params.containsKey("event")) {
			setEventID(Integer.parseInt(params.get("event")[0]));
			setEvent(eventComponent.getEvent(eventID));
		}
	}

	public void addAttendant(int value){
		Attendant att = new Attendant();
		att.setName(name);
		att.setContact(contact);
		att.setEvent(eventComponent.getEvent(value));
		attendantComponent.save(att);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/event.jsf?event="+eventID);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getEventID() {
		return eventID;
	}

	public void setEventID(int eventID) {
		this.eventID = eventID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}	
	
}
